<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>链接管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/themes/admin/css/font.css">
    <link rel="stylesheet" href="/themes/admin/css/xadmin.css">
    <link rel="stylesheet" href="/themes/admin/css/common.css">
    <script src="/themes/admin/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/themes/admin/js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="x-nav">
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-table">
                        <colgroup>
                            <col width="150">
                            <col width="200">
                            <col>
                        </colgroup>
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>链接名</th>
                            <th>URL</th>
                            <th>类别</th>
                            <td>管理</td>
                        </tr>
                        </thead>
                        <tbody>
                        {volist name="Link" id="vo"}
                        <tr>
                            <td>{$vo.link_id}</td>
                            <td><input type="text"  value="{$vo.link_title}" class="layui-input"></td>
                            <td><input type="text"  value="{$vo.link_url}" class="layui-input"></td>
                            <td>
                                <form class="layui-form">
                                    <select id="{$vo.link_id}" lay-verify="required">
                                        <option value="0">默认</option>
                                        <option {eq name='$vo.type' value='1'}selected{/eq} value="1">业务产品</option>
                                        <option {eq name='$vo.type' value='2'}selected{/eq} value="2">作品案例</option>
                                        <option {eq name='$vo.type' value='3'}selected{/eq} value="3">资讯动态</option>
                                        <option {eq name='$vo.type' value='4'}selected{/eq} value="4">关于我们</option>
                                    </select>
                                </form>
                            </td>
                            <td>
                                <button onclick="editLink({$vo.link_id},this)" class="layui-btn layui-btn-normal">保存</button>
                                <button onclick="deleteLink({$vo.link_id},this)" class="layui-btn layui-btn-danger">删除</button>
                            </td>
                        </tr>
                        {/volist}
                        <tr>
                            <td></td>
                            <td><input id="link-title" type="text" class="layui-input"></td>
                            <td><input id="link-url" type="text" class="layui-input"></td>
                            <td>
                                <form class="layui-form">
                                    <select id="link_type" lay-verify="required">
                                        <option value="0">默认</option>
                                        <option value="1">业务产品</option>
                                        <option value="2">作品案例</option>
                                        <option value="3">资讯动态</option>
                                        <option value="4">关于我们</option>
                                    </select>
                                </form>
                            </td>
                            <td>
                                <div class="layui-btn-group">
                                    <button onclick="addLink(this)" class="layui-btn layui-btn-primary"><i class="layui-icon">&#xe608;</i> 添加</button>
                                </div>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['element','layer','form']);

    //添加友情链接
    function addLink(obj){
        var linkTitle = $("#link-title").val();
        var linkUrl = $("#link-url").val();
        var linkType = $("#link_type option:selected").val();

        $.ajax({
            type:"post",
            data:
                {
                    linkTitle:linkTitle,
                    linkUrl:linkUrl,
                    type:linkType
                },
            url:"/admin/link/addLink",
            success:function(e){
                if(e.code == 1){
                    layer.msg(e.msg,{icon:1});
                    setTimeout(function () {
                        location.reload();
                    },1000)
                }else{
                    layer.msg(e.msg);
                }
            }
        });
    }
    //修改友情链接
    function editLink(linkId,obj){
        var linkTitle = $(obj).parents('tr').find('input').eq(0).val();
        var linkUrl = $(obj).parents('tr').find('input').eq(1).val();
        var linkType =  $("#"+linkId +" option:selected").val();

        $.ajax({
            type:"post",
            data: {
                linkTitle:linkTitle,
                linkUrl:linkUrl,
                type:linkType,
                linkId:linkId
            },
            url:"/admin/link/editLink",
            success:function(e){
                if(e.code == 1){
                    layer.msg(e.msg,{icon:1});
                }else{
                    layer.msg(e.msg);
                }
            }
        });
    }
    //删除友情链接
    function deleteLink(linkId,obj)
    {
        layer.msg("确定删除？" , {
            time: 0 //不自动关闭
            ,btn: ['删除', '取消']
            ,yes: function(index){
                $.ajax({
                    type:"post",
                    data:"linkId="+linkId,
                    url:"/admin/link/deleteLink",
                    success:function(e){
                        if(e.code == 1){
                            $(obj).parents('tr').remove();
                            layer.msg(e.msg,{icon:1});
                        }else{
                            layer.msg(e.msg);
                        }
                    }
                });
                layer.close(index);
            }
        });
    }
</script>
</html>